<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Service\Admin\RolesService;
use App\Http\Service\Admin\AdminService;

class AdminController extends Controller
{
    /**
     * 视图层
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        //角色数组
        /*$roles = AdminService::getRoles();*/
        $roles = RolesService::getAll();

        return view('Admin.admin.index', $roles);
    }

    /**
     * 获取列表数据
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        $get_data = $request->all();

        $result = AdminService::getList($get_data);

        return response()->json($result);
    }

    /**
     * 添加视图
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $roles = AdminService::getRoles();

        return view('Admin.admin.create', ['roles'=>$roles]);
    }

    /**
     * 添加操作
     *
     * @param Request $request
     * @return array
     */
    public function insert(Request $request)
    {
        $post_data = $request->except(['_token', 'roles']);
        $roles = $request->only(['roles']);

        shi_wu_start();
        $result = AdminService::insert($post_data, $roles);
        shi_wu_end($result);

        return $result;
    }

    /**
     * 编辑视图
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $id = $request->get('id', 0);

        $roles = AdminService::getRoles();

        $result = AdminService::edit((int)$id);

        $result['data']['roles'] = $roles;
        return view('Admin.admin.edit', $result);
    }

    /**
     * 编辑操作
     *
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $post_data = $request->except(['_token', 'roles', 'file']);
        $roles = $request->only(['roles']);

        shi_wu_start();
        $result = AdminService::update($post_data, $roles);
        shi_wu_end($result);

        return $result;
    }

    /**
     * 删除操作
     *
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        $id = $request->get('id', 0);

        return AdminService::delete((int)$id);
    }

    /**
     * 修改密码视图
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function editPassword()
    {
        return view('Admin.admin.password');
    }

    /**修改密码操作
     *
     * @param Request $request
     * @return mixed
     */
    public function updatePassword(Request $request)
    {
        $post_data = $request->only(['oldPassword', 'password', 'repassword']);

        return AdminService::updatePassword($post_data);
    }

    /**修改状态
     *
     * @param Request $request
     * @return array
     */
    public function status(Request $request)
    {
        $post_data = $request->only(['id', 'status']);

        return AdminService::editStatus($post_data);
    }
}